package com.ticket.system.service.impl;

import com.ticket.common.utils.DateUtils;
import com.ticket.system.domain.YanchuPlan;
import com.ticket.system.mapper.YanchuPlanMapper;
import com.ticket.system.service.IYanchuPlanService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 演出计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class YanchuPlanServiceImpl implements IYanchuPlanService 
{
    private static final Logger log = LoggerFactory.getLogger(YanchuPlanServiceImpl.class);
    @Autowired
    private YanchuPlanMapper yanchuPlanMapper;

    /**
     * 查询演出计划
     * 
     * @param id 演出计划主键
     * @return 演出计划
     */
    @Override
    public YanchuPlan selectYanchuPlanById(Long id)
    {
        return yanchuPlanMapper.selectYanchuPlanById(id);
    }

    /**
     * 查询演出计划列表
     * 
     * @param yanchuPlan 演出计划
     * @return 演出计划
     */
    @Override
    public List<YanchuPlan> selectYanchuPlanList(YanchuPlan yanchuPlan)
    {
        String dateYanchu = yanchuPlan.getTimePlan();
        if(StringUtils.isNotEmpty(dateYanchu)){
            if(dateYanchu.indexOf("-")<=0){
                throw new RuntimeException("参数不正确！");
            }
        }
        return yanchuPlanMapper.selectYanchuPlanList(yanchuPlan);
    }

    /**
     * 新增演出计划
     * 
     * @param yanchuPlan 演出计划
     * @return 结果
     */
    @Override
    public int insertYanchuPlan(YanchuPlan yanchuPlan)
    {
        if(yanchuPlan.getTimePlan().indexOf("-")<=0){
            throw new RuntimeException("演出时间格式不正确");
        }
        Date date = DateUtils.parseDate(yanchuPlan.getTimePlan());
        if(null == date){
            throw new RuntimeException("演出时间格式不正确");
        }
        SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy");
        String currentYear = getYearFormat.format(date);
        yanchuPlan.setYearYanchu(currentYear);
        getYearFormat = new SimpleDateFormat("MM");
        String currentMon = getYearFormat.format(date);

        yanchuPlan.setMonthYanchu(Integer.valueOf(currentMon)+"");
        return yanchuPlanMapper.insertYanchuPlan(yanchuPlan);
    }

    //集团汇总
    @Override
    public List<YanchuPlan> selectGroupList(YanchuPlan yanchuPlan){
        String dateYanchu = yanchuPlan.getTimePlan();
        if(StringUtils.isNotEmpty(dateYanchu)){
            if(dateYanchu.indexOf("-")<=0){
                throw new RuntimeException("参数不正确！");
            }
        }
        log.info("selectGroupList:{} dateYanchu:{}",yanchuPlan.toString(),dateYanchu);
        return yanchuPlanMapper.selectGroupList(yanchuPlan);
    }

    /**
     * 修改演出计划
     * 
     * @param yanchuPlan 演出计划
     * @return 结果
     */
    @Override
    public int updateYanchuPlan(YanchuPlan yanchuPlan)
    {
        if(yanchuPlan.getTimePlan().indexOf("-")<=0){
            throw new RuntimeException("演出时间格式不正确");
        }
        Date date = DateUtils.parseDate(yanchuPlan.getTimePlan());
        if(null == date){
            throw new RuntimeException("演出时间格式不正确");
        }
        SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy");
        String currentYear = getYearFormat.format(date);
        yanchuPlan.setYearYanchu(currentYear);
        getYearFormat = new SimpleDateFormat("MM");
        String currentMon = getYearFormat.format(date);

        yanchuPlan.setMonthYanchu(Integer.valueOf(currentMon)+"");
        yanchuPlan.setUpdateTime(DateUtils.getNowDate());
        return yanchuPlanMapper.updateYanchuPlan(yanchuPlan);
    }

    /**
     * 批量删除演出计划
     * 
     * @param ids 需要删除的演出计划主键
     * @return 结果
     */
    @Override
    public int deleteYanchuPlanByIds(Long[] ids)
    {
        return yanchuPlanMapper.deleteYanchuPlanByIds(ids);
    }

    /**
     * 删除演出计划信息
     * 
     * @param id 演出计划主键
     * @return 结果
     */
    @Override
    public int deleteYanchuPlanById(Long id)
    {
        return yanchuPlanMapper.deleteYanchuPlanById(id);
    }
}
